/**
 * 
 */
package dt.inlee.system.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import dt.inlee.common.exception.FrameworkException;
import dt.inlee.common.util.BaseUtil;
import dt.inlee.domain.SysMenu;
import dt.inlee.domain.SysRole;
import dt.inlee.domain.SysUser;
import dt.inlee.system.dao.SysMenuDao;
import dt.inlee.system.dao.SysRoleDao;

/**
 * @功能说明：系统角色业务层
 * @作者： 印鲜刚
 * @创建日期： 2010-5-6 @
 */
@Service("sysRoleService")
public class SysRoleService {
	@Resource(name = "sysRoleDao")
	private SysRoleDao sysRoleDao;
	@Resource(name = "sysMenuDao")
	private SysMenuDao sysMenuDao;
	/**
	 * 通过USER id获得角色列表
	 * @param id
	 * @return
	 */
	@SuppressWarnings({"rawtypes"})
    public List findRoleByUserId(String id) {
		List roleIds = new ArrayList();
		roleIds = this.sysRoleDao.findRoleByUserId(id);
		return roleIds;
	}

	/**
	 * 添加角色
	 * 
	 * @param sysRole
	 * @throws FrameworkException
	 */
	@Transactional
	public void saveSysRole(SysRole sysRole) throws FrameworkException {
		this.sysRoleDao.save(sysRole);

	}

	/**
	 * 修改角色
	 * 
	 * @param sysRole
	 * @throws FrameworkException
	 */
	@Transactional
	public void modifySysRole(SysRole sysRole) throws FrameworkException {
		this.sysRoleDao.update(sysRole);
	}

	/**
	 * 通过角色ID获取角色实体
	 * 
	 * @param sysRoleId
	 * @return
	 * @throws FrameworkException
	 */
	public SysRole findSysRoleById(String sysRoleId) throws FrameworkException {
		return (SysRole) this.sysRoleDao.get(SysRole.class, sysRoleId);
	}

	/**
	 * 通过角色ID删除角色实体
	 * 
	 * @param sysRoleId
	 * @throws FrameworkException
	 */
	@Transactional
	public void deleteSysRoleById(String sysRoleId) throws FrameworkException {
		SysRole sysRole = this.findSysRoleById(sysRoleId);
		List<SysMenu> sysMenus = sysRole.getSysMenus();
		for (SysMenu sysMenu : sysMenus) {
			sysMenu.getSysRoles().remove(sysRole);
		}
		List<SysUser> sysUsers = sysRole.getSysUsers();
		for (SysUser sysUser : sysUsers) {
			sysUser.getSysRoles().remove(sysRole);
		}
		this.sysRoleDao.delete(sysRole);
	}

	/**
	 * 通过多角色ID删除多角色实体
	 * 
	 * @param sysRoleIds
	 * @throws FrameworkException
	 */
	@Transactional
	public void deleteSysRoles(String[] sysRoleIds) throws FrameworkException {
		for (String sysRoleId : sysRoleIds) {
			this.deleteSysRoleById(sysRoleId);
		}
	}

	/**
	 * 根据条件查询角色
	 * 
	 * @param name
	 * @param currentPage
	 * @param pageSize
	 * @return
	 * @throws FrameworkException
	 */
	public List<SysRole> findAllSysRoles(String name, int currentPage,
			int pageSize,int wyId,List<SysRole> roleList) throws FrameworkException {
		return this.sysRoleDao.findAllSysRoles(name, currentPage, pageSize,wyId,roleList);
	}
	
	/**
	 * 根据条件enterpriseId查询角色
	 * @param name
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	public List<SysRole> getListSysRoles(String enterpriseId) throws FrameworkException {
		return this.sysRoleDao.getListSysRoles(enterpriseId);
	}

	/**
	 * 根据条件统计角色总数
	 * 
	 * @param name
	 * @return
	 * @throws FrameworkException
	 */
	public int getSysRoleTotalRows(String name) throws FrameworkException {
		return this.sysRoleDao.getSysRoleTotalRows(name);
	}

	/**
	 * 根据角色ID设置菜单
	 * 
	 * @param sysRoleId
	 * @param sysMenuIds
	 * @return
	 * @throws FrameworkException
	 */
	@Transactional
	public SysRole setSysMenu(String sysRoleId, String[] sysMenuIds)
			throws FrameworkException {
		SysRole sysRole = this.findSysRoleById(sysRoleId);
		List<SysMenu> sysMenus = sysRole.getSysMenus();
		for (SysMenu sysMenu : sysMenus) {
			sysMenu.getSysRoles().remove(sysRole);
		}
		if (!BaseUtil.isEmpty(sysMenuIds)) {
			for (String sysMenuId : sysMenuIds) {
				SysMenu sysMenu = (SysMenu) this.sysMenuDao.get(SysMenu.class,
						sysMenuId);
				sysMenu.getSysRoles().add(sysRole);
				sysRole.getSysMenus().add(sysMenu);
			}
		}
		this.modifySysRole(sysRole);
		return sysRole;
	}

	/**
	 * 通过角色名字查找角色
	 * 
	 * @param name
	 * @return
	 * @throws FrameworkException
	 */
	public SysRole findSysRoleByName(String name,int wyId) throws FrameworkException {
		return this.sysRoleDao.findSysRoleByName(name,wyId);
	}

	/**
	 * 根据角色ID及名字查找角色
	 * 
	 * @param id
	 * @param name
	 * @return
	 * @throws FrameworkException
	 */
	public SysRole findSysRoleByIdAndName(String id, String name,int wyId)
			throws FrameworkException {
		return this.sysRoleDao.findSysRoleByIdAndName(id, name,wyId);
	}
	
	/**
	 * 按登录者信息查询是否为市级管理员
	 * @param sysuser
	 * @return
	 */
	public boolean findIsNotManage(SysUser sysuser){
		boolean flag = false;
		List<SysRole> roles = sysuser.getSysRoles();
		List<String> list = new ArrayList<String>();
		for (SysRole sysRole : roles) {
			list.add(sysRole.getCode());
		}
		flag = list.contains("manage");
		return flag;
	}
	
	/**
	 * 按登录者信息查询是否为系统管理员
	 * @param sysuser
	 * @return
	 */
	public boolean findIsNotManageOfCompany(SysUser sysuser){
		boolean flag = false;
		List<SysRole> roles = sysuser.getSysRoles();
		for (SysRole sysRole : roles) {
			if(sysRole.getCode().startsWith("manage")){
				flag = true;
				break;
			}else{
				
			}

		}
		return flag;
	}
}
